var ws, hid, title, doctorlist;
document.addEventListener('plusready', function(){
	ws = plus.webview.currentWebview();
	hid = ws.aid;//医院id
	title = ws.title;
	console.log(hid+" "+title);
	$('.mui-title').text(title);
	//查询科室
	$.ajax({
		type:"get",
		url:apiRoot+"/home/bespoke/offices",
		data :{hid:hid},
		dataType:'json',
		success :function(data){
//			console.log(JSON.stringify(data));
			var list = '';
			var str = '';
			$.each(data['one'], function(k, v) {
				list += '<a class="mui-control-item bag '+(k == 0 ?'mui-active' : '')+'" href="#item'+(k+1)+'">'+v.classname+'</a>';
					str += '<div id="item'+(k+1)+'" class="mui-control-content '+(k == 0 ?'mui-active' : '')+'">'+
						'<ul class="mui-table-view">';
				$.each(v.two, function(k1, v1) {
					str += 	'<li class="mui-table-view-cell mui-media mui-col-9" tid="'+v1.tid+'">'+
							v1.classname+
							'</li>';
				});
				str += 	'</ul>'+
					'</div>';
			});
			$('#segmentedControls').html(list);
			$('#segmentedControlContents').html(str);
            //关闭
            $('.mui-col-9').on('tap',function(){
            	var _this = $(this);
            	$('.top-div div').eq(0).text(_this.text());
            	$('.xhua-zzao').hide();
	           	$('.zuiduo').hide();
	           	$('.mui-fullscreen').hide();
	           	search(_this.text());
            });
            //医生列表
            doctorlist = data.dlist;
            if(data.dlist.length > 0){ 
            	docList(data.dlist);
            }
		},
		error :function(e){
			errortoast();
			console.log(JSON.stringify(e));
		}
	});
},false)
//显示列表
function docList(arr){
	var str2 = '';
	$.each(arr, function(k, v) {
		str2 += '<ul class="mui-table-view bot-m" offices="'+v.offices+'" onclick=godoctor("'+v.aid+'")>'+
	        '<li class="mui-table-view-cell mui-media">'+
	            '<a href="javascript:;">'+
	                '<img class="mui-media-object mui-pull-left" src="'+getAvatar(v.avatar)+'">'+
	                '<div class="mui-media-body">'+
	                   '<span class="docter">'+v.title+'</span>&nbsp; <span>'+v.job+'</span>'+
	                    '<p ><span class="p-bot">预约量：'+v.number+'</span>'+
	                    	'<span class="mui-pull-right mui-icon iconfont">';
	                if(v.star){
						str2 += '<span class="'+(v.star >= 1 ? 'font-col' : '')+'">&#xe608;</span>';
						str2 += '<span class="'+(v.star >= 2 ? 'font-col' : '')+'">&#xe608;</span>';
						str2 += '<span class="'+(v.star >= 3 ? 'font-col' : '')+'">&#xe608;</span>';
						str2 += '<span class="'+(v.star >= 4 ? 'font-col' : '')+'">&#xe608;</span>';
						str2 += '<span class="'+(v.star >= 5 ? 'font-col' : '')+'">&#xe608;</span>';
					}else{
						str2 += '<span>&#xe608;</span>';
						str2 += '<span>&#xe608;</span>';
						str2 += '<span>&#xe608;</span>';
						str2 += '<span>&#xe608;</span>';
						str2 += '<span>&#xe608;</span>';
						str2 += '<span>&#xe608;</span>';
					}
	                str2 += '</span>'+
	                    '</p>'+
	                '</div>'+
	            '</a>'+
	        '</li>'+
	    '</ul>';
	});
	$('.dlist').html(str2);
}
//进入医生详情
function godoctor(aid){
	goNewPage('yuyue-docter.html',{aid:aid, hid:hid})
}
//按科室筛选
function search(str){
	$('.dlist ul').each(function(){
		var _this = $(this);
		var office = _this.attr('offices');
		if(office.indexOf(str) >= 0){
			_this.show();
		}else{
			_this.hide();
		}
	})
}
//按要求排序
function filter(str){
	switch(str){
		case '0':	//综合排序
		if(doctorlist.length > 0){
			docList(doctorlist);
		}
		break;
		case '1':	//预约最多
		if(doctorlist.length > 0){ 
			doctorlist.sort(function(a, b){
				return b.number - a.number;//倒序
			}); 
			docList(doctorlist);
		}
		break;
		case '2':	//评价最高
		if(doctorlist.length > 0){ 
			doctorlist.sort(function(a, b){
				return b.star - a.star;//倒序
			}); 
			docList(doctorlist);
		}
		break;
	}
}
